package com.pocketsweet.ui.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.lidroid.xutils.ViewUtils;
import com.pocketsweet.R;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.TempMain;
import com.pocketsweet.ui.uilib.ActionItem;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.ui.uilib.MyFragmentPagerAdapter;
import com.pocketsweet.ui.uilib.MyViewPager;
import com.pocketsweet.ui.uilib.TitlePopup;
import com.pocketsweet.ui.uilib.TitlePopup.OnItemOnClickListener;

public class TempHomeFragment extends Fragment implements View.OnClickListener {
	static Context mContext;
	private View rootView;
	private LinearLayout linNewOnline;
	private LinearLayout linRecomend;

	public static MyViewPager mPager;// 页卡内容
	private List<Fragment> listViews; // Tab页面列表
	public  int currIndex = 0;// 当前页卡编号
	private static TitlePopup titlePopup;
	private static int choosedSex = 0;// 0为全部；1为男；2为女

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_home, null);
		ViewUtils.inject(this, rootView);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mContext = getActivity();
		TempMain.getInstance().getHeader().init(HeaderStyle.TEXT_TITLE_IMG);

		if (currIndex == 0) {// 筛选
			if (choosedSex == 0) {
				TempMain.getInstance().getHeader()
						.setRightImg(R.drawable.icon_sex_all);
			} else if (choosedSex == 1) {
				TempMain.getInstance().getHeader()
						.setRightImg(R.drawable.icon_sex_man);

			} else if (choosedSex == 2) {
				TempMain.getInstance().getHeader()
						.setRightImg(R.drawable.icon_sex_female);
			}
			// Main.getInstance().getHeader()
			// .setRightImg(R.drawable.screen_normal);
			// } else {
			// Main.getInstance().getHeader()
			// .setRightImg(R.drawable.icon_love_call);
		}
		TempMain.getInstance().getHeader().Initcursor();
		setHeaderFunction();
		setUpComponent();
		InitViewPager();
	}

	public static void setHeaderFunction() {
		final HeaderLayout header = TempMain.getInstance().getHeader();
		header.setRighttContainerClickListener(new onRightContainerListener() {
			@Override
			public void onClick() {
				// if (currIndex == 0) {// 筛选
				// 实例化标题栏弹窗
				// 实例化标题栏弹窗
				titlePopup = new TitlePopup(TempMain.getInstance(),
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				// 给标题栏弹窗添加子类
				titlePopup.addAction(new ActionItem(TempMain.getInstance(), "  全部",
						R.drawable.icon_sex_all));
				titlePopup.addAction(new ActionItem(TempMain.getInstance(), "  男生",
						R.drawable.icon_sex_man));
				titlePopup.addAction(new ActionItem(TempMain.getInstance(), "  女生",
						R.drawable.icon_sex_female));
				titlePopup.setItemOnClickListener(new OnItemOnClickListener() {

					@Override
					public void onItemClick(ActionItem item, int position) {
						switch (position) {
						case 0:
							choosedSex = 0;
							break;
						case 1:
							choosedSex = 1;
							break;
						case 2:
							choosedSex = 2;
							break;
						default:
							break;
						}
						if (TempMain.choosedSex != choosedSex) {
							TempMain.choosedSex = choosedSex;
							Intent intent2 = new Intent();
							intent2.setAction(TempMain.ACTION_CHOOSE_CHANGE);
							TempMain.getInstance().sendBroadcast(intent2);
						}

					}
				});
				titlePopup.show(header.llRightContainer);
			}
		});
	}

	private void InitViewPager() {
		mPager = (MyViewPager) getActivity().findViewById(R.id.vPager);
		listViews = new ArrayList<Fragment>();
		listViews.add(new TempNewOnlineFragment());
		listViews.add(new TempRecommendFragment());
		mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(),
				listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener1());

	}

	private void setUpComponent() {
		View linNewOnline = getActivity().findViewById(R.id.linNewOnline);
		View linRecomend = getActivity().findViewById(R.id.linRecomend);
		linNewOnline.setOnClickListener(this);
		linRecomend.setOnClickListener(this);
	}

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener1 implements OnPageChangeListener {
		public void onPageSelected(int arg0) {
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					TempMain.getInstance().getHeader().slidleft();
				}
				if (choosedSex == 0) {
					TempMain.getInstance().getHeader()
							.setRightImg(R.drawable.icon_sex_all);
				} else if (choosedSex == 1) {
					TempMain.getInstance().getHeader()
							.setRightImg(R.drawable.icon_sex_man);
				} else if (choosedSex == 2) {
					TempMain.getInstance().getHeader()
							.setRightImg(R.drawable.icon_sex_female);
				}
				TempMain.getInstance().getHeader()
						.setRightContentShow(View.VISIBLE);
				break;
			case 1:
				if (currIndex == 0) {
					TempMain.getInstance().getHeader().slidRight();
				}
				TempMain.getInstance().getHeader()
						.setRightContentShow(View.INVISIBLE);
				break;
			}
			currIndex = arg0;
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.linNewOnline:
			mPager.setCurrentItem(0);
			if (choosedSex == 0) {
				TempMain.getInstance().getHeader()
						.setRightImg(R.drawable.icon_sex_all);
			} else if (choosedSex == 1) {
				TempMain.getInstance().getHeader()
						.setRightImg(R.drawable.icon_sex_man);
			} else if (choosedSex == 2) {
				TempMain.getInstance().getHeader()
						.setRightImg(R.drawable.icon_sex_female);
			}
			TempMain.getInstance().getHeader().setRightContentShow(View.VISIBLE);

			break;
		case R.id.linRecomend:
			mPager.setCurrentItem(1);
			TempMain.getInstance().getHeader().setRightContentShow(View.INVISIBLE);
			break;

		default:
			break;
		}

	}

}
